<%@page import="java.sql.*"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>支付成功</title>
</head>
<body>
<%
	//获取用户输入的金额、付款方式以及分期数
	String payAmount = request.getParameter("payAmount");
 	String payType = request.getParameter("payType");
 	String payDays = request.getParameter("payDays");
 	session.setAttribute("payAmount", payAmount);
	session.setAttribute("payType", payType);
	session.setAttribute("payDays", payDays);
	
	//日期
	int paydays = Integer.parseInt(payDays);
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	Date date = new Date();
	String today = sdf.format(date);
	String dueDate = sdf.format(date.getTime()+paydays*24*60*60*1000);
	session.setAttribute("today",today);
	session.setAttribute("dueDate",dueDate);
	
 	if(payType.equals("no")){
		out.print("<script type='text/javascript'>alert('请选择一款一种付款方式！');</script>");
		response.setHeader("Refresh","0.1;url=home.jsp");
	}
	String url = "jdbc:mysql://localhost:3306/wacai?serverTimezone=Asia/Shanghai";
	String dbUserName = "root";
	String dbPsd = "root1234";
	Connection connection = null;
	PreparedStatement pstmt = null;
	String sql = null;
	if(payAmount!=null && payDays!=null){
		try{
			Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
		}catch(ClassNotFoundException e){
			out.println("加载驱动器类时出现异常");
		}
	
	try{
		connection = DriverManager.getConnection(url,"root","root1234");//连接数据库
		sql = "insert into pay values(?,?,?,?,?)";
		pstmt = connection.prepareStatement(sql);
		pstmt.setString(1,payAmount);
		pstmt.setString(2,payType);
		pstmt.setString(3,payDays);
		pstmt.setString(4,today);
		pstmt.setString(5,dueDate);
		pstmt.executeUpdate();
		pstmt.close();
		connection.close();
		out.print("<script type='text/javascript'>alert('支付成功！请按时还款！');</script>");
		response.setHeader("Refresh","0.1;url=payType.jsp");
	}catch(SQLException e){
		out.print("<script type='text/javascript'>alert('支付失败！');</script>");
		response.setHeader("Refresh","0.1;url=home.jsp");
	}}%>
</body>
</html>